package org.myorg.v4;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.hdfs.protocol.DatanodeInfo;



public class RWF4 {

	private static final Log LOG = LogFactory.getLog(RWF4.class);
	
	/*
	 * read hdfs file  
	 */
	public static byte[] rhf(String path) {
		FSDataInputStream in = null;

		Configuration conf = new Configuration();
		FileSystem fs = null;
		byte[] b = null;

		try {

			fs = FileSystem.get(conf);
			Path f = new Path(path);
			in = fs.open(f);
			b = new byte[in.available()];
			in.read(b);

		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		return b;
	}
	/*
	 * write file to local file system
	 */
	public static void wlf(byte[] b, String p){
		File writefile;
		try {
			// 通过这个对象来判断是否向文本文件中追加内容
			// boolean addStr = append;
			writefile = new File(p);
			// 如果文本文件不存在则创建它
			if (writefile.exists() == false) {
				writefile.createNewFile();
				writefile = new File(p); // 重新实例化
			}
			FileOutputStream fw = new FileOutputStream(writefile,true);
			//System.out.println("###content:" + content);
			
			fw.write(b);
			fw.flush();
			fw.close();
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
	}
	
	public static void whf(String content, String path) {
		try {
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(conf);
		Path f = new Path(path);			
		FSDataOutputStream out = fs.create(f, true, 0);
		
		System.out.println("contents is:" + content);
		
		out.writeBytes(content);
		out.close();
		}
		catch(Exception e) {
			e.printStackTrace();
		}		
		
	}

	public static void execmd(String cmd) {
		Process p = null;
		try {
			p = Runtime.getRuntime().exec(cmd);
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			if (p.waitFor() == 0) {
				LOG.info("Command execute result is OK!");
			} else {
				LOG.info("Command " + cmd + " execute result is fail......");
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		p.destroy();
	}
	
	public static String mkMark(String dir, Fc4 f) {
		//for running much programmers
		File res = null; 
		String mark = "0";
		res = new File(dir);
		if (res.exists()) {
			BufferedReader input = null;
			try {
				input = new BufferedReader(new FileReader(res));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		    try {
				mark = input.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    int im = 1;
		    if(mark != null){
		    	im =  Integer.parseInt(mark);
		    }
		    
		    
		    PrintWriter output = null;
			try {
				output = new PrintWriter(new FileWriter(res));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    output.print(im+1);
		    output.close();
		} 
		else {
		    PrintWriter output = null;
			try {
				output = new PrintWriter(new FileWriter(res));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    output.print(1);
		    output.close();
		}
		
		String f_n = f.getClass().getSimpleName();
		System.out.println("caculate function " + f_n);
		
		return mark + f_n;
	}
	
	public static String mkMark(String dir, String f) {
		//for running much programmers
		File res = null; 
		String mark = "0";
		res = new File(dir);
		if (res.exists()) {
			BufferedReader input = null;
			try {
				input = new BufferedReader(new FileReader(res));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		    try {
				mark = input.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    int im = 1;
		    if(mark != null){
		    	im =  Integer.parseInt(mark);
		    }
		    
		    
		    PrintWriter output = null;
			try {
				output = new PrintWriter(new FileWriter(res));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    output.print(im+1);
		    output.close();
		} 
		else {
		    PrintWriter output = null;
			try {
				output = new PrintWriter(new FileWriter(res));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    output.print(1);
		    output.close();
		}
		
		
		System.out.println("caculate function " + f);
		
		return mark + f;
	}
	


	
}

